#include "stdafx.h"
#include "AlignStrategy.h"
#include "LetterCode.h"
#include "TextMetrics.h"

//////////////////////////////////////////////////////////////////////////////
//
//	interface
//
//////////////////////////////////////////////////////////////////////////////
bool AlignStrategy::IsRowLongerThanScreen(wstring& str, int formattingLength)
{
	return tm->CalcStringLength(str) > formattingLength;
}

int AlignStrategy::FindCuttingPoint(wstring& str, int formattingLength)
{
	return -1;
}

AlignStrategy::AlignStrategy(TextMetrics* tm)
{
	this->tm = tm;
}

int AlignStrategy::FindCuttingPointSub(wstring& str, int formattingWidth, int* cuttingPointCandidate)
{
	using namespace LetterCode;

	int i;
	int lengthSum = 0;
	bool isPrevAscii = false;

	for(i = 0; i < int(str.size()); i++)
	{
		if(IsAscii(str[i]))
		{
			if(IsTab(str[i]))
			{
				lengthSum = tm->CalcNextDocXFromTab(lengthSum);
			}
			else
			{
				lengthSum += tm->GetAsciiWidth(str[i]);
			}
			if(!isPrevAscii || IsTab(str[i])) *cuttingPointCandidate = i;
			if(str[i] == ' ' || IsPunctuation(str[i])) *cuttingPointCandidate = i + 1;
			isPrevAscii = true;
		}
		else
		{
			lengthSum += tm->GetHangeulWidth(str[i]);
			isPrevAscii = false;
		}
		if(lengthSum > formattingWidth) break;
	}
	return i;
}